using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ParticleSystem
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        ParticleSystem ps;
        ParticleSystem smoke;
        Random r = new Random();
        Matrix view;
        Matrix projection;
        Texture2D fireTexture;
        Texture2D smokeTexture;
        Effect particleEffect;

        Vector3 camEyeVector;
        Vector3 camPosition;
        Vector3 camTarget;
        Vector3 camUp;

        Texture2D smoke2;
        Texture2D fire2;
        Texture2D fire3;

        KeyboardState ks;
        float moveX = 0;
        float moveZ = 0;
        float moveSpeed = 5.0f;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        
        protected override void Initialize()
        {
            
            base.Initialize();
        }

       
        protected override void LoadContent()
        {
            
            spriteBatch = new SpriteBatch(GraphicsDevice);

            fireTexture = Content.Load<Texture2D>("fire");
            smokeTexture = Content.Load<Texture2D>("smoke");
            particleEffect = Content.Load<Effect>("ParticleEffect");

            smoke2 = Content.Load<Texture2D>("smoke2");
            fire2 = Content.Load<Texture2D>("fire2");
            fire3 = Content.Load<Texture2D>("fire3");

            camPosition = new Vector3(0, 400, 100);
            camTarget = new Vector3(0, 0, 0);
            camEyeVector = camPosition - camTarget;
            camUp = Vector3.Cross(camEyeVector, Vector3.Right);
            camUp = Vector3.Normalize(camUp);

            ps = new ParticleSystem(GraphicsDevice, fire3, particleEffect, 200, new Vector2(50), 900.0f, Vector3.Zero, 100.0f, 1.0f, 0.0f, 150);
            smoke = new ParticleSystem(GraphicsDevice, smoke2, particleEffect, 300, new Vector2(50), 1300.0f, Vector3.Zero, 500.0f, 1.0f, 0.0f, 150);


            view = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1.0f, 10000.0f);
           
        }

        
        protected override void UnloadContent()
        {
            
        }

        
        protected override void Update(GameTime gameTime)
        {
            
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            ks = Keyboard.GetState();

            if (ks.IsKeyDown(Keys.Down))
            {
                moveZ += moveSpeed;
            }

            if (ks.IsKeyDown(Keys.Up))
            {
                moveZ -= moveSpeed;
            }

            if (ks.IsKeyDown(Keys.Right))
            {
                moveX += moveSpeed;
            }

            if (ks.IsKeyDown(Keys.Left))
            {
                moveX -= moveSpeed;
            }

            ps.Update(gameTime, new Vector3(moveX, 0, moveZ));
            smoke.Update(gameTime, new Vector3(moveX, 0, moveZ));
            
            
            base.Update(gameTime);
        }

        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1, 0);


            

            smoke.Draw(view, projection, camUp, Vector3.Right, gameTime);

            ps.Draw(view, projection, camUp, Vector3.Right, gameTime);




            base.Draw(gameTime);
        }


      
    }
}
